package demoMod.stsconsole.helpers;

import demoMod.stsconsole.cards.AbstractCard;
import demoMod.stsconsole.dungeons.AbstractDungeon;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class CardHelper {
    public static void clearCardGroup(List<AbstractCard> cardList) {
        for (AbstractCard card : cardList) {
            card.onRemove();
        }
        cardList.clear();
    }

    public static void getRewardCards(int amount, List<AbstractCard> rewardList) {
        List<AbstractCard> cardPool = CardLibrary.getCards(AbstractDungeon.player.getCardColor());
        List<AbstractCard> commonList = cardPool.stream().filter(card -> card.rarity == AbstractCard.CardRarity.COMMON).collect(Collectors.toList());
        List<AbstractCard> uncommonList = cardPool.stream().filter(card -> card.rarity == AbstractCard.CardRarity.UNCOMMON).collect(Collectors.toList());
        List<AbstractCard> rareList = cardPool.stream().filter(card -> card.rarity == AbstractCard.CardRarity.RARE).collect(Collectors.toList());
        List<AbstractCard> tmpList = new ArrayList<>();
        while (tmpList.size() < amount) {
            AbstractCard.CardRarity rarity = AbstractDungeon.currDungeon.currMapNode.room.getRandomCardRarity(AbstractDungeon.cardRewardRng);
            switch (rarity) {
                case COMMON:
                    if (commonList.isEmpty()) {
                        tmpList.add(AbstractDungeon.player.getCharacterBasicCard());
                    } else {
                        tmpList.add(commonList.remove(AbstractDungeon.cardRewardRng.nextInt(commonList.size())));
                    }
                    break;
                case UNCOMMON:
                    if (uncommonList.isEmpty()) {
                        tmpList.add(AbstractDungeon.player.getCharacterBasicCard());
                    } else {
                        tmpList.add(uncommonList.remove(AbstractDungeon.cardRewardRng.nextInt(uncommonList.size())));
                    }
                    break;
                case RARE:
                    if (rareList.isEmpty()) {
                        tmpList.add(AbstractDungeon.player.getCharacterBasicCard());
                    } else {
                        tmpList.add(rareList.remove(AbstractDungeon.cardRewardRng.nextInt(rareList.size())));
                    }
                    break;
            }
        }
        for (AbstractCard card : tmpList) {
            rewardList.add(card.makeCopy());
        }
    }
}
